package com.shopping.www.service;
import com.shopping.www.mapper.UserMapper;
import com.shopping.www.model.RegisterBean;
import com.shopping.www.model.User;
import com.shopping.www.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


public class UserService {
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
        public User login(String username, String password, String status){
            SqlSession sqlSession=factory.openSession();
            UserMapper mapper=sqlSession.getMapper(UserMapper.class);
            User user =mapper.select(username, password, status);
            sqlSession.close();
            return user;
        }
        public boolean register(RegisterBean registerBean, String status){
            SqlSession sqlSession=factory.openSession();
            UserMapper mapper=sqlSession.getMapper(UserMapper.class);
            RegisterBean registerBean1=mapper.selectByUsername(registerBean.getUsername());
            User user=new User();
            user.setStatus(status);
            user.setUsername(registerBean.getUsername());
            user.setPassword(registerBean.getPassword());
            user.setId(registerBean.getId());
            user.setBanStatus(0);
            if(registerBean1==null){
                mapper.add(user);
                sqlSession.commit();
            }
            sqlSession.close();
            return registerBean1==null;
        }
}
